﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using Wsitech.Model;
using Wsitech.Utility;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     TextInfoKeyRela业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class TextInfoKeyRelaBLL : BaseBLL
    {
        private Wsitech.Persistence.Interface.ITextInfoKeyRelaDao dao = null;

        private TextInfoKeyRelaBLL()
        {
            dao = Wsitech.Persistence.Dao.TextInfoKeyRelaSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static TextInfoKeyRelaBLL instance = null;
        private static object syncRoot = new object();
        public static TextInfoKeyRelaBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new TextInfoKeyRelaBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public TextInfoKeyRela GetDetail(System.String relaid)
        {
            try
            {
                return dao.GetDetail(relaid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<TextInfoKeyRela> GetList()
        {
            try
            {
                IList<TextInfoKeyRela> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<TextInfoKeyRela> GetListByParams(object TopCounts, TextInfoKeyRela textinfokeyrela)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<TextInfoKeyRela> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="textinfokeyrela"></param>
        /// <returns></returns>
        public object Add(TextInfoKeyRela textinfokeyrela)
        {
            try
            {
                return dao.Add(textinfokeyrela);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="textinfokeyrela"></param>
        /// <returns></returns>
        public int Update(TextInfoKeyRela textinfokeyrela)
        {
            try
            {
                return dao.Update(textinfokeyrela);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="RelaId"></param>
        /// <returns></returns>
        public int Delete(System.String relaid)
        {
            try
            {
                return dao.Delete(relaid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #endregion


        /// <summary>
        /// 添加信息关键字，添加前先删除已经存在的关系 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="keywords"></param>
        public void AddRela(string textinfoid, IList<string> keywords)
        {
            base.SqlMap.BeginTransaction();
            try
            {
                //删除已有关系 
                Hashtable ht = new Hashtable();
                ht.Add("textinfoid", textinfoid);
                IList<TextInfoKeyRela> list = dao.GetListByParams(ht);
                foreach (TextInfoKeyRela rela in list)
                {
                    dao.Delete(rela.RelaId);
                }

                foreach (string key in keywords)
                {
                    //判断关键字是否存在
                    Keyword keyword = KeywordBLL.Instance.GetDetailByKey(key);
                    if (keyword == null)
                    {

                        keyword = new Keyword();
                        keyword.KeyId = Guid.NewGuid().ToString();
                        keyword.Content = key;

                        keyword.State = (int)UtilityEnum.State.Enable;
                        keyword.IsDel = (int)UtilityEnum.Del.No;

                        KeywordBLL.Instance.Add(keyword);

                    }
                    //添加关系
                    TextInfoKeyRela rela = new TextInfoKeyRela();
                    rela.RelaId = Guid.NewGuid().ToString();
                    rela.KeyId = keyword.KeyId;
                    rela.TextInfoId = textinfoid;

                    rela.State = (int)UtilityEnum.State.Enable;

                    Add(rela);

                }
                base.SqlMap.CommitTransaction();
            }
            catch (Exception)
            {
                base.SqlMap.RollBackTransaction();
                throw;
            }



        }
    }
}
